﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Catering_DBOpertion;//Please add references
namespace Catering_Operation
{
    /// <summary>
    /// 数据访问类:DailyManagement
    /// </summary>
    public partial class DailyManagement
    {
        public DailyManagement()
        { }
        #region  Method

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("id", "DailyManagement");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from DailyManagement");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(Catering_Model.DailyManagement model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into DailyManagement(");
            strSql.Append("id,ClassID,CName,Date,RID,Service,Quality,Feeding,FeedingMoney,Repair,Safety,temp1,temp2,DetermineNumber,PNumber,DishTest)");
            strSql.Append(" values (");
            strSql.Append("@id,@ClassID,@CName,@Date,@RID,@Service,@Quality,@Feeding,@FeedingMoney,@Repair,@Safety,@temp1,@temp2,@DetermineNumber,@PNumber,@DishTest)");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4),
					new SqlParameter("@ClassID", SqlDbType.Int,4),
					new SqlParameter("@CName", SqlDbType.VarChar,50),
					new SqlParameter("@Date", SqlDbType.DateTime),
					new SqlParameter("@RID", SqlDbType.Int,4),
					new SqlParameter("@Service", SqlDbType.NVarChar,4000),
					new SqlParameter("@Quality", SqlDbType.NVarChar,4000),
					new SqlParameter("@Feeding", SqlDbType.NVarChar,4000),
					new SqlParameter("@FeedingMoney", SqlDbType.Float,8),
					new SqlParameter("@Repair", SqlDbType.NVarChar,4000),
					new SqlParameter("@Safety", SqlDbType.NVarChar,4000),
					new SqlParameter("@temp1", SqlDbType.NVarChar,4000),
					new SqlParameter("@temp2", SqlDbType.NVarChar,4000),
					new SqlParameter("@DetermineNumber", SqlDbType.Int,4),
					new SqlParameter("@PNumber", SqlDbType.Int,4),
                    new SqlParameter("@DishTest", SqlDbType.NVarChar,4000)};
            parameters[0].Value = model.id;
            parameters[1].Value = model.ClassID;
            parameters[2].Value = model.CName;
            parameters[3].Value = model.Date;
            parameters[4].Value = model.RID;
            parameters[5].Value = model.Service;
            parameters[6].Value = model.Quality;
            parameters[7].Value = model.Feeding;
            parameters[8].Value = model.FeedingMoney;
            parameters[9].Value = model.Repair;
            parameters[10].Value = model.Safety;
            parameters[11].Value = model.temp1;
            parameters[12].Value = model.temp2;
            parameters[13].Value = model.DetermineNumber;
            parameters[14].Value = model.PNumber;
            parameters[15].Value = model.DishTest;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(Catering_Model.DailyManagement model, bool IsXiuGai)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into DailyManagement(");
            strSql.Append("ClassID,CName,Date,RID,Service,Quality,Repair,temp1,temp2,DetermineNumber,PNumber,DishTest)");
            strSql.Append(" values (");
            strSql.Append("@ClassID,@CName,@Date,@RID,@Service,@Quality,@Repair,@temp1,@temp2,@DetermineNumber,@PNumber,@DishTest)");
            SqlParameter[] parameters = {
					new SqlParameter("@ClassID", SqlDbType.Int,4),
					new SqlParameter("@CName", SqlDbType.NVarChar,50),
					new SqlParameter("@Date", SqlDbType.DateTime),
					new SqlParameter("@RID", SqlDbType.Int,4),
					new SqlParameter("@Service", SqlDbType.NVarChar,4000),
					new SqlParameter("@Quality", SqlDbType.NVarChar,4000),
					new SqlParameter("@Repair", SqlDbType.NVarChar,4000),
					new SqlParameter("@temp1", SqlDbType.NVarChar,4000),
                    new SqlParameter("@temp2", SqlDbType.NVarChar,4000),
					new SqlParameter("@DetermineNumber", SqlDbType.Int,4),
					new SqlParameter("@PNumber", SqlDbType.Int,4),
                    new SqlParameter("@DishTest", SqlDbType.NVarChar,4000)};
            parameters[0].Value = model.ClassID;
            parameters[1].Value = model.CName;
            parameters[2].Value = model.Date;
            parameters[3].Value = model.RID;
            parameters[4].Value = model.Service;
            parameters[5].Value = model.Quality;
            parameters[6].Value = model.Repair;
            parameters[7].Value = model.temp1;
            parameters[8].Value = model.temp2;
            parameters[9].Value = model.DetermineNumber;
            parameters[10].Value = model.PNumber;
            parameters[11].Value = model.DishTest;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Catering_Model.DailyManagement model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update DailyManagement set ");            
            strSql.Append("Service=@Service,");
            strSql.Append("Quality=@Quality");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {					
					new SqlParameter("@Service", SqlDbType.VarChar,-1),
					new SqlParameter("@Quality", SqlDbType.VarChar,-1),					
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = model.Service;
            parameters[1].Value = model.Quality;
            parameters[2].Value = model.id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool Update(int id, string type, string upvalue)
        {
            string sql = sql = "";
            if (type == "DishTest")
            {
                sql = "update DailyManagement set DishTest=@upvalue where id=@id ";
            }
            else if (type == "temp2")
            {
                sql = "update DailyManagement set temp2=@upvalue where id=@id ";
            }
            else if (type == "Repair")
            {
                sql = "update DailyManagement set Repair=@upvalue where id=@id ";
            }
            if (string.IsNullOrEmpty(sql)) return false;
            SqlParameter[] parameters = {
                    new SqlParameter("@upvalue", SqlDbType.NVarChar,4000),
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = upvalue;
            parameters[1].Value = id;
            int rows = DbHelperSQL.ExecuteSql(sql, parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from DailyManagement ");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from DailyManagement ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteListBywhere(string where)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from DailyManagement ");
            strSql.Append(" where " + where);
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Catering_Model.DailyManagement GetModel(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,ClassID,CName,Date,RID,Service,Quality,Feeding,FeedingMoney,Repair,Safety,temp1,temp2,DetermineNumber,PNumber,DishTest from DailyManagement ");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;

            Catering_Model.DailyManagement model = new Catering_Model.DailyManagement();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"] != null && ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ClassID"] != null && ds.Tables[0].Rows[0]["ClassID"].ToString() != "")
                {
                    model.ClassID = int.Parse(ds.Tables[0].Rows[0]["ClassID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["CName"] != null && ds.Tables[0].Rows[0]["CName"].ToString() != "")
                {
                    model.CName = ds.Tables[0].Rows[0]["CName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Date"] != null && ds.Tables[0].Rows[0]["Date"].ToString() != "")
                {
                    model.Date = DateTime.Parse(ds.Tables[0].Rows[0]["Date"].ToString());
                }
                if (ds.Tables[0].Rows[0]["RID"] != null && ds.Tables[0].Rows[0]["RID"].ToString() != "")
                {
                    model.RID = int.Parse(ds.Tables[0].Rows[0]["RID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Service"] != null && ds.Tables[0].Rows[0]["Service"].ToString() != "")
                {
                    model.Service = ds.Tables[0].Rows[0]["Service"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Quality"] != null && ds.Tables[0].Rows[0]["Quality"].ToString() != "")
                {
                    model.Quality = ds.Tables[0].Rows[0]["Quality"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Feeding"] != null && ds.Tables[0].Rows[0]["Feeding"].ToString() != "")
                {
                    model.Feeding = ds.Tables[0].Rows[0]["Feeding"].ToString();
                }
                if (ds.Tables[0].Rows[0]["FeedingMoney"] != null && ds.Tables[0].Rows[0]["FeedingMoney"].ToString() != "")
                {
                    model.FeedingMoney = decimal.Parse(ds.Tables[0].Rows[0]["FeedingMoney"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Repair"] != null && ds.Tables[0].Rows[0]["Repair"].ToString() != "")
                {
                    model.Repair = ds.Tables[0].Rows[0]["Repair"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Safety"] != null && ds.Tables[0].Rows[0]["Safety"].ToString() != "")
                {
                    model.Safety = ds.Tables[0].Rows[0]["Safety"].ToString();
                }
                if (ds.Tables[0].Rows[0]["temp1"] != null && ds.Tables[0].Rows[0]["temp1"].ToString() != "")
                {
                    model.temp1 = ds.Tables[0].Rows[0]["temp1"].ToString();
                }
                if (ds.Tables[0].Rows[0]["temp2"] != null && ds.Tables[0].Rows[0]["temp2"].ToString() != "")
                {
                    model.temp2 = ds.Tables[0].Rows[0]["temp2"].ToString();
                }
                if (ds.Tables[0].Rows[0]["DetermineNumber"] != null && ds.Tables[0].Rows[0]["DetermineNumber"].ToString() != "")
                {
                    model.DetermineNumber = int.Parse(ds.Tables[0].Rows[0]["DetermineNumber"].ToString());
                }
                if (ds.Tables[0].Rows[0]["PNumber"] != null && ds.Tables[0].Rows[0]["PNumber"].ToString() != "")
                {
                    model.PNumber = int.Parse(ds.Tables[0].Rows[0]["PNumber"].ToString());
                }

                if (ds.Tables[0].Rows[0]["DishTest"] != null && ds.Tables[0].Rows[0]["DishTest"].ToString() != "")
                {
                    model.DishTest = ds.Tables[0].Rows[0]["DishTest"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,ClassID,CName,Date,RID,Service,Quality,Feeding,FeedingMoney,Repair,Safety,temp1,temp2,DetermineNumber,PNumber,DishTest ");
            strSql.Append(" FROM DailyManagement ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,ClassID,CName,Date,RID,Service,Quality,Feeding,FeedingMoney,Repair,Safety,temp1,temp2,DetermineNumber,PNumber,DishTest ");
            strSql.Append(" FROM DailyManagement ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取报警信息列表
        /// </summary>
        /// <param name="pageSize">每页记录数</param>
        /// <param name="pageIndex">当前页数</param>
        /// <param name="whereClause">查询条件</param>
        /// <param name="orderClause">排序条件</param>
        /// <returns></returns>
        public DataTable GetDailyManagementList(int pageSize, int pageIndex, string whereClause, string orderClause)
        {
            string strSql = "Select * FROM (Select ROW_NUMBER() OVER(" + orderClause + ") AS rowNum,id,Date,CName,Repair,temp2,DishTest FROM DailyManagement  WITH (NOLOCK)" + whereClause + ") MyTable WHERE rowNum BETWEEN " + (((pageIndex - 1) * pageSize) + 1) + " AND " + pageIndex * pageSize;
            return DbHelperSQL.Query(strSql).Tables[0];
        }

        /// <summary>
        /// 获取报警信息列表数量
        /// </summary>
        /// <param name="whereClause"></param>
        /// <returns></returns>
        public int GetDailyManagementListCount(string whereClause)
        {
            string strSql = "Select COUNT(*) FROM DailyManagement " + whereClause;
            return Convert.ToInt32(DbHelperSQL.GetSingle(strSql));
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "DailyManagement";
            parameters[1].Value = "id";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}

